/**
 * Created by Administrator on 2017/8/22 0022.
 */
var fs = require('fs');
var GPS_POINTS = new Array();
fs.readFile('./../../ATU数据/南宁-20131206-网格1-1_GSM.txt','utf-8',function(err,data){
    if(err){
        conslole.log(err);
    }else{
       var dataString = data.toString();
        var lines = dataString.split("\r\n");
        console.log(lines.length);
        console.log(lines[0]);
        for(var i= 0,size = lines.length;i<size;i++){
            var line = lines[i].trim();
            if(startWith(line,'20')){
                var index = line.indexOf('N:');
                if(index != -1){
                    var GPS_INFO =   line.substring(index);
                    //console.log(GPS_INFO);
                    var point = transformat(GPS_INFO);
                    if(point!= null){
                        GPS_POINTS.push(point);
                    }
                }
            }
        }
    }
    console.log(GPS_POINTS[0]);
    console.log("total GPS points "+GPS_POINTS.length);
    var outPutFile = "./gpsLine.js";
    fs.writeFile(outPutFile, 'var line = '+JSON.stringify(GPS_POINTS), function(err) {
        if (err) throw err;
    console.log('The file has been saved!');
});
});


function startWith(source,str){
    if(source==null||source==""||source.length==0||str.length>source.length)
        return false;
    if(source.substring(0,str.length)==str)
        return true;
    else
        return false;
    return true;
}

function transformat(gps_isfo){
    //N:22.818384,E:108.342651,SA:6
    var temp = gps_isfo.split(',');
    if(temp.length!=3){
        return null;
    }else{
        var lat = temp[0].trim().substring(temp[0].indexOf(":")+1);
        var lon = temp[1].trim().substring(temp[0].indexOf(":")+1);
        return [parseFloat(lon),parseFloat(lat)]
    }
}